package com.boot.security.server.dao.mffl;

import java.util.List;
import java.util.Map;

import com.boot.security.server.model.mffl.MfflLawyerletter;
import com.boot.security.server.model.mffl.MfflOrder;
import com.boot.security.server.model.mffl.Vo.MfflContractSystemVo;
import com.boot.security.server.model.mffl.Vo.MfflContractVo;
import org.apache.ibatis.annotations.*;

import com.boot.security.server.model.mffl.MfflContract;

@Mapper
public interface MfflContractDao {

    @Select("select * from mffl_contract t where t.id = #{id} AND  t.deleteState = 1")
    MfflContractVo getById(Long id);

    @Select("select * from mffl_contract t where t.id = #{id}")
    MfflContractVo getByIdSystem(Long id);

    @Delete("update mffl_contract set deleteState = 0,deleteTime = now() where id = #{id}")
    int delete(Long id);

    int update(MfflContract mfflContract);
    
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into mffl_contract(userid, number, serviceId, name, serviceName, file, imageUrl, content, email, money, serviceLawyerId, payId, payNumber, payMerchantNumber, payState, state, type, typeName, createTime, updateTime, deleteState, deleteTime, paymentTime, confirmTime, courierCompanyId, courierNumber, Sender, originatingPlace, SenderPhone, addRessee, recipientPhoneNumber, address, laywerConten, lawyerimage, lawyerfile, lawyerphone, RefuseTime,commentState) values(#{userid}, #{number}, #{serviceId}, #{name}, #{serviceName}, #{file}, #{imageUrl}, #{content}, #{email}, #{money}, #{serviceLawyerId}, #{payId}, #{payNumber}, #{payMerchantNumber}, #{payState}, #{state}, #{type}, #{typeName}, #{createTime}, #{updateTime}, #{deleteState}, #{deleteTime}, #{paymentTime}, #{confirmTime}, #{courierCompanyId}, #{courierNumber}, #{Sender}, #{originatingPlace}, #{SenderPhone}, #{addRessee}, #{recipientPhoneNumber}, #{address}, #{laywerConten}, #{lawyerimage}, #{lawyerfile}, #{lawyerphone}, #{RefuseTime}, #{commentState})")
    int save(MfflContract mfflContract);
    
    int count(@Param("params") Map<String, Object> params);
    List<MfflContract> list(@Param("params") Map<String, Object> params, @Param("offset") Integer offset, @Param("limit") Integer limit);

    /**
     * 按照订单号查合同
     * @param paymerchantnumber
     * @return
     */
    @Select("select * from mffl_contract t where t.payMerchantNumber = #{paymerchantnumber} AND deleteState = 1 ")
    MfflContract getpaymerchantnumber(String paymerchantnumber);


    /**
     * 获取全部 律师已发送 律师函的订单
     * @return
     */
    @Select("select * from mffl_contract WHERE state = 3 AND deleteState = 1 AND payState = 1 AND confirmTime < NOW()")
    List<MfflContract> getAll();


    /**
     * 获取全部 律师未接单 律师函的订单
     * @return
     */
    @Select("select * from mffl_contract WHERE state = 0 AND deleteState = 1 AND payState = 1 AND RefuseTime < NOW()")
    List<MfflContract> getRefuseAll();


    /**
     * 后台列表
     */
    int contractsCount(@Param("params") Map<String, Object> params);
    List<MfflContractSystemVo> contractsList(@Param("params") Map<String, Object> params, @Param("offset") Integer offset, @Param("limit") Integer limit);

    @Update("update mffl_contract set payState = #{payState} where payMerchantNumber = #{payMerchantNumber} ")
    void udpateState(@Param("payMerchantNumber") String  payMerchantNumber, @Param("payState") int payState);


    /**
     * 定时任务
     * 获取满足条件的数据（具体配置在mffl_service服务表中配置）
     *      1.用户发起合同，律师未点击接收的
     *      2.律师点击完成服务，用户未点击确认完成的
     */
    @Select("SELECT a.id,a.userId,a.payMerchantNumber,a.serviceId,a.serviceLawyerId,a.state \n" +
            "FROM mffl_contract AS a,mffl_order AS b \n" +
            "WHERE a.payMerchantNumber = b.pay_merchant_number AND a.payState = b.state AND a.payState = 1 AND a.deleteState = 1 AND ( a.state = 0 OR a.state = 3 ) AND\n" +
            "IF(a.state = 0,\n" +
            "		IF(a.serviceId = 4,\n" +
            "		(UNIX_TIMESTAMP()- UNIX_TIMESTAMP( a.paymentTime ))* 1000 > #{ContractAudit_RefuseTime} ,\n" +
            "		(UNIX_TIMESTAMP()- UNIX_TIMESTAMP( a.paymentTime ))* 1000 > #{ContractCustomization_RefuseTime} ),\n" +
            "		IF(a.serviceId = 4,\n" +
            "		(UNIX_TIMESTAMP()- UNIX_TIMESTAMP( a.confirmTime ))* 1000 > #{ContractAudit_ConfirmTime},\n" +
            "		(UNIX_TIMESTAMP()- UNIX_TIMESTAMP( a.confirmTime ))* 1000 > #{ContractCustomization_ConfirmTime} )\n" +
            ") FOR UPDATE")
    List<MfflContract> getTimerTaskData(@Param("ContractAudit_ConfirmTime") Long ContractAudit_ConfirmTime,@Param("ContractAudit_RefuseTime") Long ContractAudit_RefuseTime,
                                        @Param("ContractCustomization_ConfirmTime") Long ContractCustomization_ConfirmTime,@Param("ContractCustomization_RefuseTime") Long ContractCustomization_RefuseTime);
}